package likouJava;

import java.util.*;
import java.util.stream.Collectors;

public class 删除相同字符 {
    public static List<String> remove(List<String> list){
        return new ArrayList<>(new LinkedHashSet<>(list));
    }

    public static List<String> topAge(Map<String,Integer> map){
        return map.entrySet().stream()
                .sorted((e1,e2)-> e2.getValue().compareTo(e1.getValue()))
                .limit(3)
                .map(Map.Entry::getKey)
                .collect(Collectors.toList());
    }

    public static Map<String,Integer>count(List<String>list){
        Map<String,Integer>counts = new LinkedHashMap<>();
        for(String s: list){
            counts.put(s,counts.getOrDefault(s,0) + 1);
        }
        return counts;
    }

    public static Map<String,List<String>> splitNum(List<String>list){
        List<String> numbers = new ArrayList<>();
        List<String> letters = new ArrayList<>();
        for(String s : list){
            if(s.matches("\\d+")){
                numbers.add(s);
            }else if (s.matches("[a-zA-Z]+")){
                letters.add(s);
            }
        }
        return Map.of("数字列表",numbers,"字符列表",letters);
    }

    public static Map<String,Double>calculate(Map<String,List<Integer>> grades){
        Map<String,Double>result = new HashMap<>();
        grades.forEach((name,scores) ->
                result.put(name,scores.stream().mapToInt(Integer::intValue).average().orElse(0.0)
                ));
        return result;

    }

    public static Map<String,String>convert(List<String>list){
        Map<String,String> map = new HashMap<>();
        for(String s: list){
            String []parts = s.split(",",2);
            if(parts.length == 2){
                map.put(parts[1].trim(), parts[0].trim());
            }
        }
        return map;
    }


   public static void main(String[] args) {
        List<String> test1 = List.of("apple","banana","apple","orange","banana","grape","grape");
        System.out.println("第一题输出:" + remove(test1));

        Map<String,Integer> test2 = new HashMap<>();
        test2.put("John",35);
        test2.put("Bob",40);
        test2.put("Alice",30);
        test2.put("Tom",45);
        test2.put("Jerry",50);
        System.out.println("第二题输出"+topAge(test2));

       System.out.println("第三题输出"+count(test1));

        List<String> test4 = List.of("123","abc","456","def","789","ghi");
        System.out.println("题四输出:"+splitNum(test4));

        Map<String,List<Integer>>test5 =new HashMap<>();
        test5.put("John",List.of(85,90,95));
        test5.put("Bob",List.of(80,85,90));
        test5.put("Alice",List.of(95,100,105));
        System.out.println("第五题输出:"+calculate(test5));


        List<String> test6 = List.of(
                "John Doe,john@doe.com",
                "Bob Smith,bob@smith.com",
                "Alice Johnson,alice@johnson.com"
        );
        System.out.println("第六题输出:" + convert(test6));

   }
}
